//
//  HHLeadInPagesViewController.m
//  LeadInPages
//
//  Created by 花花 on 2016/12/11.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHLeadInPagesViewController.h"

@interface HHLeadInPagesViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) UIPageControl *pageControl;
@property (assign, nonatomic) NSInteger count;

@property(nonatomic, copy) setupCellHandler setupCellHandler;
@property(nonatomic, copy) finishbtnOnClick finishbtnOnClick;
@end
static NSString *const CellID = @"CellID";
@implementation HHLeadInPagesViewController

/*
 count :总图片数量
 setupCellHandler : 定义了一个block 两个参数 cell indexPath
 
 finishbtnOnClick: 完成按钮的点击事件
 */
-(instancetype)iniitWithPagesCount:(NSUInteger)count setupCellHandler:(setupCellHandler)setupCellHandler finishOnClick:(finishbtnOnClick)finishbtnOnClick{

    if (self == [super initWithNibName:nil bundle:nil]) {
        _count = count;
        _setupCellHandler =[setupCellHandler copy];
        _finishbtnOnClick =[finishbtnOnClick copy];
        
        //使用懒加载初始化
        [self.view addSubview:self.collectionView];
        [self.view addSubview:self.pageControl];
        //注册cell
        [self.collectionView registerClass:[HHLeadInPagesCell class ] forCellWithReuseIdentifier:CellID];
        
    }

    return self;

}
#pragma mark - <UICollectionViewDataSource,UICollectionViewDelegate>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.count;

}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HHLeadInPagesCell *cell  =[collectionView dequeueReusableCellWithReuseIdentifier:CellID forIndexPath:indexPath];
    if (indexPath.row == self.count - 1) {//如果是最后一页，则添加响应事件
        
        cell.finishbtn.hidden = NO;
        [cell.finishbtn  addTarget:self action:@selector(finishOnlick:) forControlEvents:UIControlEventTouchUpInside];
    }else{ //如果不是最后一面则隐藏按钮
    
        cell.finishbtn.hidden = YES;
    
    }
    if (self.setupCellHandler) {
        self.setupCellHandler(cell,indexPath);
    }
    
    return cell;

}

-(void)finishOnlick:(UIButton *)btn{

    if (self.finishbtnOnClick) {
        self.finishbtnOnClick(btn);
    }
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = self.view.bounds.size;
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        UICollectionView *collectionView =[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = collectionView;
    }
    
    return _collectionView;

}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger currentIndex = scrollView.contentOffset.x / scrollView.bounds.size.width +0.5;
    if (self.pageControl.currentPage != currentIndex) {
        self.pageControl.currentPage = currentIndex;
    }
    
}
-(UIPageControl *)pageControl{

    if(!_pageControl){
    
        UIPageControl *pageControl =[[UIPageControl alloc]init];
        pageControl.numberOfPages = self.count;
        pageControl.currentPage = 0;
        
        CGSize pageSize =[pageControl sizeForNumberOfPages:self.count];
        CGFloat pageControlX = (self.view.bounds.size.width - pageSize.width)/2;
        CGFloat pageControlY =(self.view.bounds.size.height -pageSize.height -50);
        pageControl.frame = CGRectMake(pageControlX, pageControlY, pageSize.width, pageSize.height);
        _pageControl = pageControl;
    }

    return _pageControl;


}
@end
